import type {
  ISpaServiceFromServer,
  ISpaServiceListItemFromServer
} from '@/api/spa/interfaces/spa';
import type { DetailsResponse, ListResponse, Response } from '@/http';
import { request } from '@/http';
import type { IBanner } from '@/api/spa/interfaces/banner';

/**
 * 获取海报集合
 */
export async function getBanners() {
  const res = await request.post<ListResponse<IBanner>>('/spa/index/getBanner');
  return res.data.list.map((item: IBanner) => item.banner_image);
}

/**
 * 获取服务列表
 * @param {IGetServicesPayload} payload
 */
export async function getServices(payload: IGetServicesPayload): Promise<IGetServicesResult> {
  const res = await request.post<Response<{ list: IGetServicesResult }>>(
    '/spa/service/getList',
    payload
  );
  return res.data.list;
}

export interface IGetServicesPayload {
  pagination: boolean;
  page: number;
  pageSize: number;
}

export interface IGetServicesResult {
  data: ISpaServiceListItemFromServer[];
  current_page: number;
  last_page: number;
  per_page: number;
  total: number;
}

/**
 * 获取服务详情
 * @param {number} id
 */
export async function getService(id: number) {
  const res = await request.post<DetailsResponse<ISpaServiceFromServer>>('/spa/service/getDetail', {
    id
  });
  return res.data.detail;
}

/**
 * 检查库存
 * @param {ICheckInventoryPayload} payload
 */
export async function checkInventory(payload: ICheckInventoryPayload) {
  const res = await request.post<Response<{ time_list: ICheckInventoryResult[] }>>(
    '/spa/service/checkStocks',
    payload
  );
  return res.data.time_list.filter((item) => item.canChoose).map((item) => item.time);
}

export interface ICheckInventoryPayload {
  serviceId: number;
  serviceType: number;
  goodsId: number;
  bookDate: string;
}

export interface ICheckInventoryResult {
  time: string;
  canChoose: boolean;
}
